home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / video / fly8111-.000 / fly8111- / fly8 / shapes / f8shape.awk < prev    next >
Text File  |  1979-12-31  |  5KB  |  313 lines

  1. # --------------------------------- f8shape.awk -------------------------------
  2.  
  3. #
  4. # This is part of the flight simulator 'fly8'.
  5. # Author: Eyal Lebedinsky (eyal@ise.canberra.edu.au).
  6. #
  7.  
  8. #
  9. # Convert a (cpp processed) object description to a simple list of numbers.
  10. # The input should be a comma separated list of quadruplets.
  11. # Evaluates expressions using '+ - * / % ( )'. All other characters are 
  12. # removed. A vertex count is added to the beginning.
  13. #
  14.  
  15.  
  16. BEGIN    {
  17.     name = ARGV[1]
  18.     fin  = name ".i"
  19.     fout = name ".003"
  20.  
  21.     lineno = 0
  22.     n = 0
  23.     advance()
  24.     while (1) {
  25.         x = int(expression())
  26.         eat(",")
  27.  
  28.         y = int(expression())
  29.         eat(",")
  30.  
  31.         z = int(expression())
  32.         eat(",")
  33.  
  34.         f = int(expression())
  35.         if (0 == f)
  36.             break;
  37.  
  38.         printf ("%d %d %d %d\n", x, y, z, f) >fout
  39.         ++n
  40.  
  41.         if (tok == "(eof)")
  42.             break
  43.         eat(",")
  44.     }
  45.  
  46.     close(fin)
  47.     close(fout)
  48.  
  49.     fin  = name ".003"
  50.     fout = name ".vxx"
  51.  
  52.     print n, 2 >fout        # V_METERS = 2
  53.     while (getline<fin != 0) {
  54.         print $0 >fout
  55.     }
  56.  
  57.     close(fin)
  58.     close(fout)
  59. }
  60.  
  61. function advance() {
  62.     if (tok == "(eof)")
  63.         return tok
  64.     while (1) {
  65.         while (1) {
  66.             if (length(line) == 0 || line ~ /^static VERTEX/)
  67.                 ++lineno
  68.             else if (substr(line,1,1) == "#") {
  69.                 split(line,a)
  70.                 if (a[1] == "#line")
  71.                     lineno = a[2]+0
  72.                 else
  73.                     ++lineno
  74.             } else
  75.                 break
  76.             if (getline line <fin == 0)
  77.                 return tok = "(eof)"
  78.         }
  79.         if (match(line, /^[ \t;\n{}]/))
  80.                 line = substr(line, 2)
  81.         else
  82.                 break
  83.     }
  84.     if (match(line, /^[0-9]+/) ||
  85.         match(line, /^(<=|==|!=|>=|\|\||&&|>>|<<)/) ||
  86.         match(line, /^./)) {
  87.             tok = substr(line, 1, RLENGTH)
  88.             line = substr(line, RLENGTH+1)
  89.             return tok
  90.     }
  91.     error("line " lineno " incomprehensible at " line)
  92. }
  93.  
  94. function eat(s) {
  95.     if (tok != s)
  96.         error("line " lineno ": saw \"" tok "\" expected \"" s "\"")
  97.     advance()
  98. }
  99.  
  100. function error(s) {
  101.     print "Error: " s
  102.     exit 1
  103. }
  104.  
  105. function expression(  e, e1, e2) {
  106.     e = logical_or()
  107.     if (tok == "?") {
  108.         advance()
  109.         e1 = expression()
  110.         eat(":")
  111.         e2 = expression()
  112.         if (e)
  113.             e = e1
  114.         else
  115.             e = e2
  116. # This fails!:    e = e ? e1 : e2
  117.     }
  118.     return e
  119. }
  120.  
  121. function logical_or(  e, e1) {
  122.     e = logical_and()
  123.     while (tok == "||") {
  124.         advance()
  125.         e1 = logical_and()
  126.         e = e || e1
  127.     }
  128.     return e
  129. }
  130.  
  131. function logical_and(  e, e1) {
  132.     e = inclusive_or()
  133.     while (tok == "&&") {
  134.         advance()
  135.         e1 = inclusive_or()
  136.         e = e && e1
  137.     }
  138.     return e
  139. }
  140.  
  141. function bitwise(a, b, op,  abit, bbit, asign, bsign, rbit, r, t, n) {
  142.     a = int(a)
  143.     asign = (a < 0) ? 1 : 0;
  144.     b = int(b)
  145.     bsign = (b < 0) ? 1 : 0;
  146.     r = 0
  147.     rpos = 1
  148.     for (n = 0; n < 32; ++n) {
  149.         t = int((a-asign)/2)
  150.         abit = a != t+t
  151.         a = t
  152.         t = int((b-bsign)/2)
  153.         bbit = b != t+t
  154.         b = t
  155.         if (op == "|")
  156.             rbit = abit || bbit
  157.         else if (op == "&")
  158.             rbit = abit && bbit
  159.         else if (op == "^")
  160.             rbit = abit != bbit
  161.         else # unary ~
  162.             rbit = !bbit
  163.         r += rpos*rbit
  164.         rpos += rpos
  165.     }
  166.     return r
  167. }
  168.  
  169. function inclusive_or(  e, e1) {
  170.     e = exclusive_or()
  171.     while (tok == "|") {
  172.         advance()
  173.         e1 = exclusive_or()
  174.         e = bitwise(e, e1, "|");
  175.     }
  176.     return e
  177. }
  178.  
  179. function exclusive_or(  e, e1) {
  180.     e = and()
  181.     while (tok == "^") {
  182.         advance()
  183.         e1 = and()
  184.         e = bitwise(e, e1, "^");
  185.     }
  186.     return e
  187. }
  188.  
  189. function and(  e, e1) {
  190.     e = equality()
  191.     while (tok == "&") {
  192.         advance()
  193.         e1 = equality()
  194.         e = bitwise(e, e1, "&");
  195.     }
  196.     return e
  197. }
  198.  
  199. function equality(  op, e, e1) {
  200.     e = relational()
  201.     while (tok == "==" || tok == "!=") {
  202.         op = tok
  203.         advance()
  204.         e1 = relational()
  205.         if (op == "==")
  206.             e = e == e1
  207.         else
  208.             e = e != e1
  209.     }
  210.     return e
  211. }
  212.  
  213. function relational(  op, e, e1) {
  214.     e = shift()
  215.     while (tok ~ /<|<=|>=|>/) {
  216.         op = tok
  217.         advance()
  218.         e1 = shift()
  219.         if (op == "<")
  220.             e = e < e1
  221.         else if (op == "<=")
  222.             e = e <= e1
  223.         else if (op == ">=")
  224.             e = e >= e1
  225.         else
  226.             e = e > e1
  227.     }
  228.     return e
  229. }
  230.  
  231. function shift(  op, e, e1) {
  232.     e = additive()
  233.     while (tok == "<<" || tok == ">>") {
  234.         op = tok
  235.         advance()
  236.         e1 = additive()
  237.         if (op == ">>")
  238.             e1 = -e1
  239.         if (e1 > 0) {
  240.             for (e1 = int(e1); e1 > 0; --e1)
  241.                 e *= 2;
  242.         } else {
  243.             for (e1 = int(-e1); e1 > 0; --e1)
  244.                 e /= 2;
  245.         }
  246.     }
  247.     return e
  248. }
  249.  
  250. function additive(  op, e, e1) {
  251.     e = multiplicative()
  252.     while (tok == "+" || tok == "-") {
  253.         op = tok
  254.         advance()
  255.         e1 = multiplicative()
  256.         if (op == "+")
  257.             e = e + e1
  258.         else
  259.             e = e - e1
  260.     }
  261.     return e
  262. }
  263.  
  264. function multiplicative(  op, e, e1) {
  265.     e = unary()
  266.     while (tok == "*" || tok == "/" || tok == "%") {
  267.         op = tok
  268.         advance()
  269.         e1 = unary()
  270.         if (op == "*")
  271.             e = e * e1
  272.         else if (op == "/")
  273.             e = e / e1
  274.         else
  275.             e = e % e1
  276.     }
  277.     return e
  278. }
  279.  
  280. function unary() {
  281.     if (tok == "-") {
  282.         advance()
  283.         return -primary()
  284.     } else if (tok == "~") {
  285.         advance()
  286.         return bitwise(0, primary(), "~")
  287.     } else if (tok == "!") {
  288.         advance()
  289.         return !primary()
  290.     } else if (tok == "+") {
  291.         advance()
  292.         return primary()
  293.     } else
  294.         return primary()
  295. }
  296.  
  297. function primary(  e) {
  298.     if (tok == "(") {
  299.         advance()
  300.         e = expression()
  301.         eat(")")
  302.         return e
  303.     }
  304.  
  305.     if (tok ~ /^[0-9]+/) {
  306.         e = tok
  307.         advance()
  308.         return e+0
  309.     }
  310.  
  311.     error("unexpected \"" tok "\" at line " lineno)
  312. }
  313.